﻿using System;
using System.Collections.Generic;
using System.Text;
using TekaMUD.Engine.IO;

namespace TekaMUD.Engine.Blocks
{
	public class HealthData
	{
		private const float DEHYDRATION_RATE = 1f;
		private RationalAgent ra;

		public int Health { get; set; }
		public uint MaxHealth { get; set; }
		public int Mana { get; set; }
		public uint MaxMana { get; set; }
		public int Stamina { get; set; }
		public uint MaxStamina { get; set; }
		public int HP_Cur { get; set; }
		public int HP_Max { get; set; }
		public int HP_Min { get; set; }
		public int Stamina_Max { get; set; }
		public int Stamina_Cur { get; set; }
		public int Hydration { get; set; }
		public int Hydration_Max { get; set; }

		public HealthData(RationalAgent ra)
		{
			this.ra = ra;
		}

		/// <summary>
		/// Updates player's health according to the passage of time.
		/// </summary>
		public void UpdateHealth()
		{
			// check for death conditions.
			// TODO.

			// Dehydrate
			if(Hydration > 0)
				Hydration--;

			if(ra is Player)
			{
				Player p = ra as Player;

				if (Hydration < 10)
					Messenger.SendAgent(p, "You are in GRAVE need of water!");
				else if (Hydration < 20)
					Messenger.SendAgent(p, "You are extremely dehydrated!");
				else if (Hydration < 40)
					Messenger.SendAgent(p, "You are very thirsty!");
				else if (Hydration < 60)
					Messenger.SendAgent(p, "You are feeling thirsty.");
			}

			double thirst_penalization = 10 / (1 + Math.Exp((-(Hydration_Max - Hydration) + 60) / 8));

			// Penalize player's HP accordingly.
			HP_Cur -= (int) Math.Floor(thirst_penalization);
			

			// Recovery actions

			// Recover stamina. This should also work exponentially or accordint to HP and hydration Values.
			if (Stamina_Cur < Stamina_Max)
				Stamina_Cur++;

			// Hit points, up to  90% of max, unless is an NPC, in which case they recover fully :D
			if (ra is NPC)
			{
				if (HP_Cur < HP_Max)
					HP_Cur++;
			}
			else
			{
				if (HP_Cur < HP_Max && ((HP_Cur / HP_Max < 0.9))) 
					HP_Cur++;
			}
		}
	}
}